<template>
	<view class="content">
		<view class="caption"> 员工图片 </view>
		<view class="cu-form-group upload">
			<view class="solids" @tap="ChooseImage()">
				<image mode="aspectFill" :src="imgList"></image>
			</view>
		</view>
		<view class="caption"> 员工基本信息 </view>
		<view class="cu-form-group">
			<view class="title">姓名</view>
			<input  v-model="employeesName" placeholder="请输入姓名" name="input" type="text"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">电话</view>
			<input placeholder="请输入电话" name="input" v-model="tel" type="number"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">类别</view>
			<input placeholder="请输入类别" name="input" v-model="employeesType" type="text"></input>
		</view>
		<view class="next" @tap="addStaff">
			<view class="next2">确认</view>
		</view>
	</view>
</template>

<script>
	import {addStaff,showStaffInfoById} from '@/api/index.js';
	import config from '@/config/index';
	var sourceType = [
	    ['camera'],
	    ['album'],
	    ['camera', 'album']
	]
	var sizeType = [
	    ['compressed'],
	    ['original'],
	    ['compressed', 'original']
	]
	var _self;
	export default {
		data() {
			return {
				imgList: '../../../static/images/addPic.png',
				employeesName:null,//员工姓名
				employeesType:null,//员工类型
				tel:null,//员工电话
				employeesId:null,//员工id
				sourceTypeIndex: 2,
				sourceType: ['拍照', '相册', '拍照或相册'],
				sizeTypeIndex: 2,
				sizeType: ['压缩', '原图', '压缩或原图'],
				countIndex: 8,
				count: [1, 2, 3, 4, 5, 6, 7, 8, 9],
			}
		},
		onLoad(options) {
			this.employeesId=options.id;
			if(this.employeesId!=null){
				this.showStaffInfo()
			}
		},
		methods: {
			//根据id查询员工
			showStaffInfo(){
				let _that = this
				showStaffInfoById(this.employeesId).then(re =>{
					_that.imgList = re.image
					_that.employeesName = re.name
					_that.employeesType = re.empType
					_that.tel = re.tel
				}).catch(re=>{
					console.log(re);
				});
			},
			//添加员工
			addStaff(){
				let _that = this
				if(_that.imgList == '../../../static/images/addPic.png'){
					this.$api.msg('员工图片不能为空');
					return false
				}
				if(_that.employeesName == ''){
					this.$api.msg('员工名称不能为空');
					return false
				}
				if(_that.employeesType == ''){
					this.$api.msg('员工类型不能为空');
					return false
				}
				if(_that.tel == ''){
					this.$api.msg('员工电话不能为空');
					return false
				}
				if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(_that.tel))){
					this.$api.msg('请输入正确的电话号码');
					return false
				}
				addStaff(_that.employeesId,_that.imgList,_that.employeesName,_that.employeesType,_that.tel).then(re =>{
					if(_that.employeesId!=undefined){
						uni.showToast({
						    title: '编辑员工成功',
						    duration: 2000
						});
					}else{
						uni.showToast({
						    title: '添加员工成功',
						    duration: 2000
						});
					}
					setTimeout(function(){
						uni.navigateBack({
						    delta: 1
						});
					},2000)
				}).catch(re=>{
					console.log(re);
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			//选择图片
			ChooseImage: async function() {
				let _that = this
				uni.chooseImage({
					count: 1, //最多可以选择的图片张数，默认9
					sizeType: ["compressed"], //original 原图，compressed 压缩图，默认二者都有
					sourceType: ["album"], //album 从相册选图，camera 使用相机，
					success(res) {
						// 获得临时路径 
						var tempFilePath = res.tempFilePaths;
						uni.uploadFile({
							url: config.requestUrl + '/uploadCt/uploadImg',
							filePath: tempFilePath[0],
							name: 'file',
							
							header:{
								'Content-Type': 'application/x-www-form-urlencoded',
								'userToken':uni.getStorageSync('token'),
							},
							success: (res) => {
								let json = JSON.parse(res.data)
								_that.imgList=json.result.imgSrc;
							}
						});
					}
				})
			},
			async checkPermission(code) {
			    let type = code ? code - 1 : this.sourceTypeIndex;
			    let status = permision.isIOS ? await permision.requestIOS(sourceType[type][0]) :
			        await permision.requestAndroid(type === 0 ? 'android.permission.CAMERA' :
			            'android.permission.READ_EXTERNAL_STORAGE');
			
			    if (status === null || status === 1) {
			        status = 1;
			    } else {
			        uni.showModal({
			            content: "没有开启权限",
			            confirmText: "设置",
			            success: function(res) {
			                if (res.confirm) {
			                    permision.gotoAppSetting();
			                }
			            }
			        })
			    }
			    return status;
			},
		}
	}
</script>

<style>
	.caption{color: #424242;font-weight: 600;padding:24rpx;}
	.upload{padding:11rpx;}
	.cu-form-group input{text-align: right;}
	.next{
		margin:0 76rpx;
	}
	.next .next2{
		background: url("../../../static/images/band-card.png") no-repeat;
		background-size: 100% 100%;
		color: #fff;	
		width:600rpx;
		height:80rpx;
		margin:130rpx auto 0;
		line-height: 80rpx;
		text-align: center;
		font-size: 28rpx;
	}
	.solids{
		width:160rpx;
		height: 160rpx;
	}
	.solids image{
		width:160rpx;
		height: 160rpx;
	}
</style>
